//
//  ZYGesturePasswordVC.m
//  YiLin
//
//  Created by Zyark on 2018/5/31.
//  Copyright © 2018年 Zyark. All rights reserved.
//

#import "ZYGesturePasswordVC.h"
#import "ZYSetGesturePasswordVC.h"
#import "PCCircleViewConst.h"

@interface ZYGesturePasswordVC ()<UITableViewDelegate,UITableViewDataSource,ZYSetGesturePasswordVCDelegate>
@property(nonatomic,strong) UITableView *tableView;
@property(nonatomic,strong) NSArray *titleArray;

/**
  是否开启手势密码
 */
@property(nonatomic,strong) UISwitch *onGesturePasswordSwitch;

/**
 是否显示轨迹
 */
@property(nonatomic,strong) UISwitch *onDisplayTrackSwitch;
@end

@implementation ZYGesturePasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.title = [[ZYChangeLanguage sharedZYChangeLanguage] getStringByKey:LC_ME_MAIN_SET_GESTURE_PASSWORD];
//    if(![[PCCircleViewConst getGestureWithKey:gestureFinalSaveKey] length]){
//        [_onGesturePasswordSwitch setOn:NO];
//        [ZYConfig setIsOnGesturePassword:NO];
//    }
    [_onGesturePasswordSwitch setOn:[ZYConfig isOnGesturePassword]];
}

#pragma mark -init
-(void)setup{
    _onGesturePasswordSwitch = [self setupSwitch];
    [_onGesturePasswordSwitch addTarget:self action:@selector(onGesturePassword:) forControlEvents:UIControlEventTouchUpInside];
    _onDisplayTrackSwitch = [self setupSwitch];
     [_onDisplayTrackSwitch addTarget:self action:@selector(onDisplayTrack:) forControlEvents:UIControlEventTouchUpInside];
    _onGesturePasswordSwitch.on = [ZYConfig isOnGesturePassword];
    _onDisplayTrackSwitch.on = [ZYConfig isDisplayTrack];
    [self setupTableView];
    [self setupUserInfo];
}

-(void)setupTableView{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ZYSCREEN_W, ZYSCREEN_H - self.navigationView.height)];
    [self.view addSubview:_tableView];
    _tableView.top = self.navigationView.bottom;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollEnabled = NO;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    //    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView reloadData];
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

-(UISwitch *)setupSwitch{
    UISwitch *switchBtn = [[UISwitch alloc] init];
    [switchBtn setOnTintColor:kMainColor];
    return switchBtn;
}

/**
 tableview cell 右边switch
 @param cell cell
 @return label
 */
-(UILabel *)setupSwitch:(UISwitch *)switchBtn cell:(UITableViewCell *)cell{
    UILabel *detailLabel = [[UILabel alloc] init];
    [cell addSubview:switchBtn];
    [switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(cell).offset(-15);
        make.centerY.equalTo(cell.mas_centerY);
    }];
    return detailLabel;
}

-(void)setupUserInfo{

    [_tableView reloadData];
}

#pragma mark - TableViewDelegate datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:ID];
    }
    //    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    //    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    cell.backgroundColor = [UIColor whiteColor];
    //手势密码 显示轨迹 修改手势密码
    cell.textLabel.text = self.titleArray[indexPath.row];
    
    //            cell.detailTextLabel.textAlignment = NSTextAlignmentLeft;
    if(indexPath.row < 2){
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if(indexPath.row == 0){
            [self setupSwitch:_onGesturePasswordSwitch cell:cell];
        }else{
            [self setupSwitch:_onDisplayTrackSwitch cell:cell];
        }
    }else{
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    return cell;
}

#pragma mark - TableView delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if(indexPath.row == 2){
        //跳转到修改手势密码界面
        ZYSetGesturePasswordVC *vc = [[ZYSetGesturePasswordVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

#pragma mark - Getter
-(NSArray *)titleArray{
    if(!_titleArray){
        ZYChangeLanguage *language = [ZYChangeLanguage sharedZYChangeLanguage];
        //手势密码 显示轨迹 修改手势密码
        _titleArray = @[[language getStringByKey:LC_ME_MAIN_MODIFY_GESTURE_PASSWORD_LABEL],[language getStringByKey:LC_ME_MAIN_MODIFY_DISPLAY_TRACK],[language getStringByKey:LC_ME_MAIN_MODIFY_EDIT_GESTURE_PASSWORD_LABEL]];
    }
    return _titleArray;
}

#pragma mark - Event
-(void)onGesturePassword:(UISwitch *)sender{
    if(sender.on){
        //跳转到修改手势密码界面
        ZYSetGesturePasswordVC *vc = [[ZYSetGesturePasswordVC alloc] init];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    [ZYConfig setIsOnGesturePassword:sender.on];
}

-(void)onDisplayTrack:(UISwitch *)sender{
    [ZYConfig setIsDisplayTrack:sender.on];
}

#pragma mark - ZYSetGesturePasswordVCDelegate
-(void)setGesturePasswordSuccess:(ZYSetGesturePasswordVC *)setGesturePasswordVC{
    if(![_onGesturePasswordSwitch isOn]){
        [_onGesturePasswordSwitch setOn:YES];
    }
}
@end
